from re import search
from time import sleep

from selenium.webdriver.common.by import By

from base.base import Base


class BdPage(Base):
    # 定位器
    click_kpl_loc=(By.XPATH,'//a[@href="/f?kw=kpl"]')
    click_kpl_zm_loc=(By.LINK_TEXT,'【吧务招募】KPL吧吧务招募')
    search_is_loc=(By.ID,'wd1')
    user_loc=(By.XPATH,'//input[@name="userName" and @id="TANGRAM__PSP_5__userName"]')
    pwd_loc=(By.XPATH,'/html/body/div[5]/div/div[2]/div/div[4]/div[2]/div[1]/div[1]/form/p[8]/input[2]')
    dx_loc=(By.NAME,'memberPass')
    dl_button_loc=(By.CSS_SELECTOR,'.pass-button.pass-button-submit')
    dy1_loc=(By.ID,'post_content_148617317027')
    # 百度一下，找到相关贴吧贴子0篇，用时0.288秒
    dy2_loc=(By.CLASS_NAME,'s_nav_right')
    # 定位封装
    def click_kpl(self):
        self.click(self.click_kpl_loc)
    def click_zm(self):
        self.click(self.click_kpl_zm_loc)
    def dy1(self):
        sleep(5)
        return self.text(self.dy1_loc)
    def sosuo_suiji(self):
        self.send_keys(self.search_is_loc,'sjfoijewoifjwoifbhbgihgiuow')
        self.ENTER(self.search_is_loc)
    def dy2(self):
        return self.text(self.dy2_loc)
    def send_user(self,user):
        self.send_keys(self.user_loc,user)
    def send_pwd(self,pwd):
        self.send_keys(self.pwd_loc, pwd)
    def dx_click(self):
        self.click(self.dx_loc)
    def click_dl(self):
        self.click(self.dl_button_loc)
    # 定位逻辑
    def search_is(self):
        self.open_url('https://tieba.baidu.com/')
        sleep(5)
        self.re_web()
        try:
            self.sosuo_suiji()
        except Exception as e:
            print('由于无法定位,刷新重试')
            self.re_web()
            self.sosuo_suiji()
        assert '百度一下，找到相关贴吧贴子0篇' in self.dy2()
    def login(self,user,pwd):
        self.kpl_tie()
        try:
            self.send_user(user)
            self.send_pwd(pwd)
            self.dx_click()
            self.click_dl()
        except Exception as e:
            self.re_web()
            sleep(2)
            self.send_user()
            self.send_pwd()
            self.dx_click()
            self.click_dl()
    def kpl_tie(self):
        self.open_url('https://tieba.baidu.com/')
        sleep(5)
        self.re_web()
        try:
            self.click_kpl()
        except Exception as e:
            print('因为无法定位到元素,所以进行刷新')
            self.re_web()
        self.qh()
        sleep(5)
        self.re_web()
        self.click_zm()
        self.qh()
        assert '为促进吧内的发展，共同为整顿本吧风气而奉献出自己的一份力量' in self.dy1